package com.hsq.jdbc.resultset_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class ResultSet_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        //得到statement
        Statement statement = connection.createStatement();

        String sql = "select id, name, sex, borndate from student";
        //执行给定的SQL语句，该语句返回单个ResultSet对象
        ResultSet resultSet = statement.executeQuery(sql);
        //5. 使用while取出数据
//        +----+--------+-----+---------------------+
//        | id | name   | sex | borndate
//        +----+--------+-----+---------------------
//        |  1 | 鞠婧祎 | 女  | 1990-05-13 00:00:00
//        |  2 | 邓紫棋 | 女  | 1990-05-13 00:00:00
//        +----+--------+-----+--------------------
        while (resultSet.next()) {//让光标向后移动，如果没有更多行则返回false
            int id = resultSet.getInt(1); //返回第1列
            String name = resultSet.getString(2);
            String sex = resultSet.getString(3);
            Date birthday = resultSet.getDate(4);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + birthday);
        }
        //关闭
        resultSet.close();
        statement.close();
        connection.close();
    }
}
